package org.eclipse.epsilon.emc.jdt;

import java.util.ArrayList;
import java.util.HashSet;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.epsilon.common.dt.launching.dialogs.AbstractModelConfigurationDialog;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;

/**
 * This is the dialogue for JDT model configuration. It provides 
 * three features (the first two are inherited from the super class):
 * <p>
 * 1. setting the model name;<p>
 * 2. setting the alias;<p>
 * 3. selecting the projects to be parsed.<p>
 * 
 * @author Cheng Yun
 *
 */
public class JdtModelConfig extends AbstractModelConfigurationDialog {
	private List list;
	private	Button selectAll;
	private Button deselectAll;
	private HashSet<String> projects = new HashSet<String>();
	public static final String PROJECTS = "projects selected";
	
	@Override
	protected String getModelName() {
		return "JDT Model";
	}

	@Override
	protected String getModelType() {
		return "JDT";
	}
	
	private void createProjectSelectionGroup(Composite parent) {
		final Composite groupContent = createGroupContainer(parent, "Projects", 1);
		
		getList(groupContent);
		createRadioButtonGroup(groupContent);
		groupContent.layout();
	}
	
	private void createRadioButtonGroup(Composite parent){
		final Composite groupContent = createGroupContainer(parent, "", 2);
		getSelectButton(groupContent);
		getDeSelectButton(groupContent);
		groupContent.layout();
	}
	
	private List getList(Composite groupContent){
		list = new List(groupContent,SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
		
		//project list
		final ArrayList<String> projectNameList = new ArrayList<String>();
		java.util.List<IJavaProject> javaProjects = getJavaProjects();
		for(IJavaProject javaProject : javaProjects){
			projectNameList.add(javaProject.getProject().getName());
		}
		
		list.setItems(projectNameList.toArray(new String[projectNameList.size()]));
		list.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
				
		list.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event){
				String[] selected = list.getSelection();
				if(selected.length>0){
					for(int i=0;i<selected.length;i++){
						String temp = selected[i];
						for(String str : projectNameList){
							if(str.equals(temp)){
								projects.add(str);
							}						
						}					
					}
				}
			}
		});
		
		return list;
	}
	
	private Button getSelectButton(Composite groupContent){
		selectAll = new Button (groupContent, SWT.RADIO);
		
		//selection radio buttons
		selectAll.setText ("Select All");
		selectAll.addListener (SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				if(selectAll.getSelection()==true){
					list.selectAll();
					deselectAll.setSelection(false);
				}
			}
		});
		return selectAll;
	}
	
	private Button getDeSelectButton(Composite groupContent){
		deselectAll = new Button (groupContent, SWT.RADIO);
		
		//selection radio buttons
		deselectAll.setText ("Deselect All");
		deselectAll.addListener (SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				if(deselectAll.getSelection()==true){
					list.deselectAll();
					selectAll.setSelection(false);
					projects.clear();
				}
			}
		});
		return deselectAll;
	}
	
	private java.util.List<IJavaProject> getJavaProjects(){
		java.util.List<IProject> projects = JdtManipulator.getIProjects();
		try {
			//get all java projects
			return JdtManipulator.getIJavaProjects(projects);
		} catch (CoreException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	protected void createGroups(Composite control) {
		createNameAliasGroup(control);
		createProjectSelectionGroup(control);
	}
	
	protected void loadProperties() {
		super.loadProperties();
		if (properties == null) return;
		//get selected projects'names
		String [] selection = properties.getProperty(PROJECTS).split(",");
		list.setSelection(selection);
		
	}
	
	protected void storeProperties() {
		super.storeProperties();
		String projectStr = "";
		//put the names of selected projects in a string, separated by commas
		for (String str : projects) {
			if (projectStr.equals(""))
				projectStr = str;
			else
				projectStr = projectStr + "," + str;
		}
		super.properties.put(PROJECTS, projectStr);
	}
}
